package com.aabte.mybatis.mapper.custom.mapper;

import org.apache.ibatis.mapping.MappedStatement;
import tk.mybatis.mapper.entity.EntityColumn;
import tk.mybatis.mapper.mapperhelper.EntityHelper;
import tk.mybatis.mapper.mapperhelper.MapperHelper;
import tk.mybatis.mapper.mapperhelper.MapperTemplate;
import tk.mybatis.mapper.mapperhelper.SqlHelper;
import tk.mybatis.mapper.util.StringUtil;

import java.util.Set;

public class MyBatchUpdateProvider extends MapperTemplate {

    public MyBatchUpdateProvider(Class<?> mapperClass, MapperHelper mapperHelper) {
        super(mapperClass, mapperHelper);
    }

    /**
     * <foreach collection="list" item="record" separator=";">
     * UPDATE tbl_emp
     * <set>
     * <if test="record.empName != null">
     * emp_name=#{record.empName},
     * </if>
     * <if test="record.empAge != null">
     * emp_age=#{record.empAge},
     * </if>
     * <if test="record.empSalary != null">
     * emp_salary=#{record.empSalary},
     * </if>
     * </set>
     * where emp_id=#{record.empId}
     * </foreach>
     *
     * @return
     */
    public String batchUpdate(MappedStatement ms) {

        String entityName = "record";

        Class<?> entityClass = super.getEntityClass(ms);
        String tableName = super.tableName(entityClass);
        String updateClause = SqlHelper.updateTable(entityClass, tableName);

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("<foreach collection=\"list\" item=\"record\" separator=\";\">");

        // UPDATE tbl_emp ,注意：无需手动拼接update
        stringBuilder.append(updateClause);

        String idColumn = null;
        String idColumnHolder = null;

        // 生成SET标签
        stringBuilder.append("<set>");
        Set<EntityColumn> columns = EntityHelper.getColumns(entityClass);
        for (EntityColumn entityColumn : columns) {
            // 判断是否是主键
            if (entityColumn.isId()) {
                idColumn = entityColumn.getColumn();
                idColumnHolder = entityColumn.getColumnHolder(entityName);
                continue;
            }

            stringBuilder.append("<if test=\"");
            if (StringUtil.isNotEmpty(entityName)) {
                stringBuilder.append(entityName).append(".");
            }
            stringBuilder.append(entityColumn.getProperty()).append(" != null");
            if (entityColumn.getJavaType().equals(String.class)) {
                stringBuilder.append(" and ");
                if (StringUtil.isNotEmpty(entityName)) {
                    stringBuilder.append(entityName).append(".");
                }
                stringBuilder.append(entityColumn.getProperty()).append(" != '' ");
            }
            stringBuilder.append("\">");
            stringBuilder.append(entityColumn.getColumnEqualsHolder(entityName)).append(",");
            stringBuilder.append("</if>");
        }
        stringBuilder.append("</set>");

        // 生成where
        stringBuilder.append("where ")
                .append(idColumn)
                .append("=")
                .append(idColumnHolder);

        stringBuilder.append("</foreach>");

        return stringBuilder.toString();
    }
}
